package org.jeecg.modules.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.storage.entity.PackageStorage;
import org.jeecg.modules.storage.entity.PackageStorageMaterial;
import org.jeecg.modules.storage.mapper.PackageStorageMapper;
import org.jeecg.modules.storage.mapper.PackageStorageMaterialMapper;
import org.jeecg.modules.storage.service.IPackageStorageService;
import org.jeecg.modules.storage.service.IProcurementManagementService;
import org.jeecg.modules.storage.vo.PackageStorageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 包材入库单
 * @Author: jeecg-boot
 * @Date:   2024-08-05
 * @Version: V1.0
 */
@Service
public class PackageStorageServiceImpl extends ServiceImpl<PackageStorageMapper, PackageStorage> implements IPackageStorageService {

	@Autowired
	private PackageStorageMapper packageStorageMapper;
	@Autowired
	private PackageStorageMaterialMapper packageStorageMaterialMapper;

	@Autowired
	private IProcurementManagementService iProcurementManagementService;

	@Override
	public IPage<PackageStorageVo> pagelist(Page<PackageStorageVo> page, QueryWrapper<PackageStorageVo> queryWrapper) {
		return this.baseMapper.pagelist(page,queryWrapper);
	}


	@Override
	public void retract(String id) {
		PackageStorage packageStorage = this.packageStorageMapper.selectById(id);
		packageStorage.setStatus("0");
		this.packageStorageMapper.updateById(packageStorage);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(PackageStorage packageStorage, List<PackageStorageMaterial> packageStorageMaterialList) {
//		packageStorage.setStatus("1");
		packageStorageMapper.insert(packageStorage);
		if(packageStorageMaterialList!=null && packageStorageMaterialList.size()>0) {
			for(PackageStorageMaterial entity:packageStorageMaterialList) {
				//外键设置
				entity.setPackageStorageId(packageStorage.getId());
				if(!StringUtils.isEmpty(entity.getProcurementDetailId())) {
					iProcurementManagementService.addNumProcurement(entity);
				}
				packageStorageMaterialMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(PackageStorage packageStorage,List<PackageStorageMaterial> packageStorageMaterialList) {
		packageStorageMapper.updateById(packageStorage);

		iProcurementManagementService.subtractNumProcurement(packageStorageMaterialList);
		//1.先删除子表数据
		packageStorageMaterialMapper.deleteByMainId(packageStorage.getId());

		//2.子表数据重新插入
		if(packageStorageMaterialList!=null && packageStorageMaterialList.size()>0) {
			for(PackageStorageMaterial entity:packageStorageMaterialList) {
				//外键设置
				entity.setPackageStorageId(packageStorage.getId());
				if(!StringUtils.isEmpty(entity.getProcurementDetailId())){
					iProcurementManagementService.addNumProcurement(entity);
				}
				packageStorageMaterialMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		List<PackageStorageMaterial> packageStorageMaterials = this.packageStorageMaterialMapper.selectList(new LambdaQueryWrapper<PackageStorageMaterial>()
				.eq(PackageStorageMaterial::getPackageStorageId, id).select(PackageStorageMaterial::getId));
		List<String> strlist = packageStorageMaterials.stream().map(PackageStorageMaterial::getId).collect(Collectors.toList());
		this.delBatchMain(strlist);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
//			packageStorageMaterialMapper.deleteByMainId(id.toString());
//			packageStorageMapper.deleteById(id);
			this.deleteData((String)id);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	private void deleteData(String id){
		PackageStorageMaterial packageStorageMaterial = this.packageStorageMaterialMapper.selectById(id);
		PackageStorage packageStorage = this.packageStorageMapper.selectById(packageStorageMaterial.getPackageStorageId());
		if(packageStorage.getStatus().equals("0") || packageStorage.getStatus().equals("2")){
			throw new RuntimeException("入库单已审批或已入库");
		}
		this.packageStorageMaterialMapper.deleteById(id);
		Long count = this.packageStorageMaterialMapper.selectCount(new LambdaQueryWrapper<PackageStorageMaterial>()
				.eq(PackageStorageMaterial::getPackageStorageId, packageStorage.getId()));
		if(count == 0){
			this.packageStorageMapper.deleteById(packageStorage.getId());
		}
		List<PackageStorageMaterial> list = new ArrayList<>();
		list.add(packageStorageMaterial);
		iProcurementManagementService.subtractNumProcurement(list);
	}
	
}
